﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using System;
using System.Activities;
using System.Activities.Presentation;
using System.Activities.Presentation.Model;
using System.Activities.Presentation.Services;
using System.Activities.Presentation.ViewState;
using System.Activities.Validation;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.RemoteCtrls
{
    [Serializable]
    public class ValidationInfo
    {
        internal ValidationInfo(ValidationError error)
        {
            var parentProperty = error.Source.GetType().GetProperty("Parent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            
            if (parentProperty != null)
            {
                var parentobj = parentProperty.GetValue(error.Source);
                
                if (parentobj != null)
                {
                    var parentIdRef = WorkflowViewState.GetIdRef(parentobj);
                    var idProperty = parentobj.GetType().GetProperty("Id", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                    var nameProperty = parentobj.GetType().GetProperty("DisplayName", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                    if (idProperty != null && nameProperty != null)
                    {
                        var id = idProperty.GetValue(parentobj);
                        var name = nameProperty.GetValue(parentobj);
                        ActivityParentId = id.ToString();
                        ActivityParentName = name.ToString();
                        ActivityParentIdRef = parentIdRef;
                    }
                }
            }
            var idRef = WorkflowViewState.GetIdRef(error.Source);
            if (idRef!=null)
            {
                ActivityParentIdRef = idRef;
            }
            Message = error.Message;
            IsWarning = error.IsWarning;
            PropertyName = error.PropertyName;
            Id = error.Id;
        }
        internal ValidationInfo(Exception exception)
        {
            Message = exception.Message;
            IsException = true;
        }

        public string Message
        {
            get;
            private set;
        }

        public bool IsWarning
        {
            get;
            private set;
        }

        public bool IsException
        {
            get;
            private set;
        }

        public string PropertyName
        {
            get;
            private set;
        }

        public string Id
        {
            get;
            private set;
        }

        /// <summary>
        /// 活动组件父类ID
        /// </summary>
        public string ActivityParentId
        {
            set;
            get;
        }
        /// <summary>
        /// 活动组件父类IDRef
        /// </summary>
        public string ActivityParentIdRef
        {
            set;
            get;
        }
        
        /// <summary>
        /// 活动文件名称
        /// </summary>
        public string FileName
        {
            set;
            get;
        }
        /// <summary>
        /// 活动父类名称
        /// </summary>
        public string ActivityParentName
        {
            set;
            get;
        }
    }
}
